<template>
  
  <div class="mblog" v-loading="loading">
    <!-- 标题 -->
    <h1 class="b-header">{{blog.title}}</h1>
    <!-- 信息 -->
    <div class="sub-title">
      <div class="time">日期：{{$filters.dateFormat2(blog.created)}}</div>
      <div class="desc">摘要：{{blog.desc}}</div>
    </div>
    <!-- 内容 -->
    <div class="content" v-html="blog.content"></div>
    
    <!-- 评论 -->
    <comment></comment>
  </div>
</template>

<script>
  import {getBlogById} from 'network/app'
  import Comment from './components/Comment'
  export default {
    name: "BlogDetail.vue",
    components: {Comment},
    data() {
      return {
        blog: {
          id: "",
          title: "",
          content: "",
          created: '',
          desc: '',
        },
        loading: false
      }
    },
    created() {
      const blogId = this.$route.params.blogId
      const _this = this
      this.loading = true
      getBlogById(blogId).then(res => {

        const blog = res.data.data
        _this.blog.id = blog.id
        _this.blog.title = blog.title
        _this.blog.created = blog.created
        _this.blog.desc = blog.description
        var md = require("markdown-it")({
          html: true,
          typographer: true,
          highlight: function (str, lang) {
            // 此处判断是否有添加代码语言
            if (lang && hljs.getLanguage(lang)) {
              try {
                // 得到经过highlight.js之后的html代码
                const preCode = hljs.highlight(lang, str, true).value
                // 以换行进行分割
                const lines = preCode.split(/\n/).slice(0, -1)
                // 添加自定义行号
                let html = lines.map((item, index) => {
                  return '<li><span class="line-num" data-line="' + (index + 1) + '"></span>' + item + '</li>'
                }).join('')
                html = '<ol>' + html + '</ol>'
                // 添加代码语言
                if (lines.length) {
                  html += '<b class="name">' + lang + '</b>'
                }
                return '<pre class="hljs"><code>' +
                  html +
                  '</code></pre>'
              } catch (__) {}
            }
          // 未添加代码语言，此处与上面同理
            const preCode = md.utils.escapeHtml(str)
            const lines = preCode.split(/\n/).slice(0, -1)
            let html = lines.map((item, index) => {
              return '<li><span class="line-num" data-line="' + (index + 1) + '"></span>' + item + '</li>'
            }).join('')
            html = '<ol>' + html + '</ol>'
            return '<pre class="hljs"><code>' +
              html +
              '</code></pre>'
          }
        })
        
        var result = md.render(blog.content)

        _this.blog.content = result

      })
      .finally(()=>{
        this.loading = false
      })
    }
  }
</script>

<style lang="scss" scoped>
  /* .mblog {
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    width: 100%;
    min-height: 700px;
    padding: 20px 15px;
  } */
  .mblog{
    letter-spacing: .5px;
    padding: .5rem 1rem;
    border-bottom: 1px solid #d3d3d3;
    .b-header{
      font-size: .45rem;
      line-height: .45rem;
      padding-bottom: .37037rem;
      font-weight: bold;
      border-bottom: 1px solid #d3d3d3;
      overflow: hidden;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    .sub-title{
      color: #556677;
      font-size: .296296rem;
      line-height: .296296rem;
      padding: .185185rem .37037rem .555556rem;
      line-height: .555556rem;
      div{
        margin-bottom: .185185rem;
      }
    }
    .content{
      color: #111;
      border-top: 1px solid #d3d3d3;
      padding: .185185rem .37037rem;
    }
    
  }
  @media (max-width: 916px) {
    .mblog{
      padding: .5rem .3rem;
      .content{
        padding: .185185rem .27037rem;
      }
    }
    .nav{
      display: none;
    }
  }
</style>

<style lang="scss">
  .content{
    font-size: .296296rem;
    line-height: .555556rem;
    h1,h2,h3,h4,h5,h6{
      font-weight: bold;
      border-bottom: 1px solid #d3d3d3;
      margin: .185185rem 0;
      padding-bottom: .185185rem;
    }
    h1{
      font-size: .555556rem;
      line-height: .555556rem;
      color: #303133;
    }
    h2{
      font-size: .518519rem;
      line-height: .518519rem;
      color: rgba(48,49,51,.9);
    }
    h3{
      font-size: .481481rem;
      line-height: .481481rem;
      color: rgba(48,49,51,.8);
    }
    h4{
      font-size: .444444rem;
      line-height: .444444rem;
      color: rgba(48,49,51,.7);
    }
    h5{
      font-size: .407407rem;
      line-height: .407407rem;
      color: rgba(48,49,51,.6);
    }
    h6{
      font-size: .37037rem;
      line-height: 20px;
      color: rgba(48,49,51,.5);
    }
    pre{
     
      margin-top: .185185rem;
      margin-bottom: .185185rem;
    }
    p{
      overflow: auto;
    }
    p>code,li>code{
      
      background-color: #eee;
      padding: .037037rem .092593rem;
      border-radius: .092593rem;
      
    }
    ul,ol{
      margin-left: 2em;
    }
    li>ul,li>ol{
      margin-left: 1em;
    }
    blockquote{
      // margin-left: 2em;
      padding: .37037rem 0 .185185rem .185185rem;
      margin: .37037rem 0;
      border-left: .092593rem solid #ccc;
      color: #556677;
    }
    code{
      font-family: monospace,Consolas,Monaco,"Andale Mono";;
    }
    a{
      color: #1a0dab;
      text-decoration: none;
    }
    a:hover{
      text-decoration: underline;
    }
    img{
      max-width: 100%;    
      border: 2px solid #ccc;
      box-sizing: border-box;
      border-radius: .185185rem;
      margin: .185185rem 0;
      display: block;
    }

    table{
        border-collapse: collapse;
        margin: 0 auto;
        text-align: center;
    }
    table td, table th
    {
        border: 1px solid #cad9ea;
        color: #666;
        height: 30px;
    }
    table thead th
    {
        background-color: #CCE8EB;
        width: 100px;
    }
    table tr:nth-child(odd)
    {
        background: #fff;
    }
    table tr:nth-child(even)
    {
        background: #F5FAFA;
    }
    li{
        margin: .5rem 0;
    }

    pre.hljs {
    padding: 8px 2px;
    border-radius: 5px;
    position: relative;
    ol {
      list-style: decimal;
      margin: 0;
      margin-left: 40px;
      padding: 0;
      li {
        margin: 0;
        list-style: decimal-leading-zero;
        position: relative;
        padding-left: 10px;
        .line-num {
          position: absolute;
          left: -40px;
          top: 0;
          width: 40px;
          height: 100%;
          border-right: 1px solid rgba(0, 0, 0, .66);
        }
      }
    }
    b.name {
      position: absolute;
      top: 2px;
      right: 12px;
      z-index: 10;
      color: #999;
      pointer-events: none;
    }
  }
  }
 
</style>